package com.lelefly.base.serviceImp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.lelefly.base.daoimp.BaseDao;
import com.lelefly.base.daoimp.LeleFlyInfoDao;
import com.lelefly.base.model.FlyDetailBean;
import com.lelefly.base.model.FlyHistoryPage;
import com.lelefly.base.model.LeleFlyHistoryDetailBean;
import com.lelefly.base.model.Result;
import com.lelefly.base.po.LeleFlyInfo;
import com.lelefly.base.po.LeleflyHistoryInfo;
import com.lelefly.constant.Constant;

@Service
public class LeleFlyInfoService extends BaseService{

	@Resource
	private BaseDao baseDao;
	
	@Resource
	private LeleFlyInfoDao leleFlyInfoDao;
	
	public Result queryRealTimeFly(String planeId){
		Result result = new Result();
		String sql = "select MAX(CAST(flightime AS UNSIGNED)) as flightime from lelefly_fly_info where planeid='" + planeId + "'";
		try {
			LeleFlyInfo info = leleFlyInfoDao.getLastTimeFly(sql);
			long currentTime = System.currentTimeMillis();
			if (info == null) {
				result.mes = "未查询到记录";
			}else {
				long lastTimeLong = Long.parseLong(info.getFlightime());
				if (currentTime - lastTimeLong > (20*1000)) {// 距离上次上传间隔20s以上，认为飞机不是在实时飞行
					result.mes = "飞机当前不在飞行";
				}
			}
			result.state = Constant.HTTP_STATUS_OK;
		} catch (Exception e) {
			result.state = Constant.HTTP_STATUS_FAIL;
			result.mes = "查询错误";
		}
		return result;
	}
	
	/**
	 * 查询飞行记录
	 * @param planeId
	 * 飞机ID
	 * @return
	 */
	public Result<LeleflyHistoryInfo> queryFlyHistory(FlyHistoryPage page){
		Result<LeleflyHistoryInfo> result = new Result<>();
		
		try {
			String hql = "from LeleflyHistoryInfo where planeId='" + page.getPlaneId() + "' order by flyBeginTime DESC";
			List<LeleflyHistoryInfo> list = leleFlyInfoDao.queryHistoryInfos(hql,page.getPage(),page.getCount());
			result.setCurPage(page.getPage());
			result.setMes("查询成功");
			result.setResultList(list);
			int size = leleFlyInfoDao.queryObjetReList(hql).size();
			result.setAllCount(size);
			result.setState(Constant.HTTP_STATUS_OK);
			int count = page.getCount();
			int totalPage = size%count == 0 ? size/count:size/count+1;
			result.setTotalPage(totalPage);
			
		} catch (Exception e) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("服务器内部错误");
		}
		return result;
	}
	
	/**
	 * 查询飞行详细信息
	 * @param bean
	 * @return
	 */
	public Result<LeleFlyHistoryDetailBean> queryFlyDetailInfo(FlyDetailBean bean){
		Result<LeleFlyHistoryDetailBean> result = new Result<>();
		String sql = "select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_1 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+ "select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_2 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+"select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_3 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+"select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_4 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+"select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_5 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+"select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_6 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+"select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_7 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+"select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_8 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+"select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_9 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+"select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_10 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+"select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_11 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "' union all "
				+"select planeid as planeid,flightime as flightime,mobile_phone_no as mobilePhoneNo,user_phone_no as userPhoneNo,latitude as latitude,longitude as longitude,flightmode as flightmode,radar_height as radarHeight,barometer_height as barometerHeight,speed as speed,flow_data as flowData,gps_no as gpsNo,radio as radio,batt_volt as battVolt,history_id as historyId from lelefly_fly_info_12 where planeid='"
				+ bean.getPlaneId() + "' and history_id='" 
				+ bean.getHistoryId() + "'";
		try {
			List<LeleFlyHistoryDetailBean> list = leleFlyInfoDao.queryFlyDetail(sql);
			result.setMes("查询成功");
			result.setState(Constant.HTTP_STATUS_OK);
			result.setResultList(list);
		} catch (Exception e) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("查询失败");
		}
		return result;
	}
}
